﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
using Medianamik.Core.Sugar;
using System.Collections.Specialized;

namespace Medianamik.Core
{
    /// <summary>
    /// Classe représantant une Property d'un Type Medianamik
    /// </summary>
    [DebuggerDisplay("{Name}")]
    public class NodeTypeInterfaceProperty : SubEntity<NodeTypeInterfaceProperty, NodeTypeInterface>, IProperty
    {
        public NodeTypeInterfaceProperty(Guid id, string name, string description,
            Guid interfaceId, Guid dataTypeId, short? maxLength, Guid? primaryTypeId)
            : base(id)
        {
            Init(interfaceId, name, description, dataTypeId, maxLength, primaryTypeId);
        }

        public NodeTypeInterfaceProperty(string name, string description, Guid interfaceId,
           Guid dataTypeId, short? maxLength, Guid? primaryTypeId)
        {
            Init(interfaceId, name, description, dataTypeId, maxLength, primaryTypeId);
        }

        private void Init(Guid interfaceId, string name, string description, Guid dataTypeId, short? maxLength, Guid? primaryTypeId)
        {
            OwnerId = interfaceId;
            Name = name;
            Description = description;
            DataTypeId = dataTypeId;
            MaxLength = maxLength;
            PrimaryTypeID = primaryTypeId;
        }

        public string Description { get; set; }

        private string _name;
        public String Name
        {
            get
            {
                if (_name == null && Status == EntityStates.New)
                {
                    throw new CoreException("Entity is not loaded.");
                }

                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public string LocalizedName
        {
            get
            {
                if (Status != EntityStates.New)
                {
                    return (string)System.Web.HttpContext.GetGlobalResourceObject("TypeManagement",
                        ID + "_Label");
                }

                return string.Empty;
            }
        }

        public Guid DataTypeId
        {
            get;
            set;
        }

        private DataType _dataType;
        public DataType DataType
        {
            get
            {
                if (_dataType == null)
                {
                    _dataType = ConfigManager.GetDataTypeById(DataTypeId);
                }
                return _dataType;
            }
        }

        protected override NodeTypeInterface GetOwner(Guid ownerId)
        {
            return NodeTypeManager.GetNodeTypeInterface(ownerId);
        }

        public short? MaxLength
        {
            get;
            set;
        }

        public Guid? PrimaryTypeID
        {
            get;
            set;
        }
    }
}
